<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="/static/common/taglib.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>车次管理</title>
    <%@include file="../../public/commonCss.jsp" %>
    <%@include file="../../public/commonJs.jsp" %>
</head>
<body ms-controller="main">
<div id="wrapper">
    <div class="boxmain whiteBox lrbox">
        <div class="lrboxleft borderR" style="width:60%">
            <div class="from-header borderB contentbox">
                <div>
                    <span class="font14"> 所属分类:</span><a id="category"><span class=" fontOrange2">车次管理</span></a>
                    <button class="layui-btn layui-btn-normal layui-btn-small" style="float:right;margin-left:20px"
                            ms-on-click="loadSearchData()">搜索
                    </button>
                    <input type="text" style="float:right;margin-left:20px" class="inputText" placeholder="搜索车次，始发站，终点站"
                           title="搜索车次，始发站，终点站" ms-duplex="searchKey.code"/>
                    <button class="layui-btn layui-btn-normal layui-btn-small" style="float:right;" ms-on-click="addRecord()">新增车次</button>
                </div>
            </div>
            <div class="from-body withheader contentbox" style=" overflow: auto;">
                <table class="layui-table">
                    <thead>
                    <tr >
                        <th>行号</th>
                        <th>交路名称</th>
                        <th>车次</th>
                        <th>始发站</th>
                        <th>到达站</th>
                        <th>始发时间</th>
                        <th>到达时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody ms-each="datas">
                    <tr ms-on-click="selectWorks(el)">
                        <td>{{(currentPage-1)*pageSize+$index+1}}</td>
                        <td>{{el.roadName}}</td>
                        <td>{{el.code}}</td>
                        <td>{{el.startCity}}</td>
                        <td>{{el.endCity}}</td>
                        <td>{{el.startTime}}</td>
                        <td>{{el.endTime}}</td>
                        <td>
                            <button class="layui-btn layui-btn-normal layui-btn-mini" ms-on-click="editRecord(el)">编辑</button>
                            <button class="layui-btn layui-btn-mini" ms-on-click="copyRecord(el)">复制</button>
                            <button class="layui-btn layui-btn-danger layui-btn-mini"  ms-on-click="deleteRecord(el)">删除</button>
                        </td>
                    </tr>
                    </tbody>
                </table>
                <!-- 分页 -->
                <!-- 分页 -->
                <div id="demo" class="fenye">
                </div>

            </div>
        </div>
        <div class="lrboxmain borderL" style="margin-left:60%">
            <div class="from-header borderB ">
                <span class="font14">当前车次:<span class=" fontOrange2">{{trainCode}}</span></span>
            </div>
            <div class="from-body withheader contentbox8" style=" overflow: auto;">
                <table class="layui-table" style="width: 40%;float:left;">
                    <thead>
                    <tr>
                        <th>行号</th>
                        <th>站点名称</th>
                        <th>到达时间</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr ms-repeat="stations">
                        <td>{{$index+1}}</td>
                        <td>{{el.name}}</td>
                        <td>{{el.arriveTime}}</td>
                    </tr>
                    </tbody>
                </table>
                <table class="layui-table" style="width: 58%;float:left;margin-left: 10px">
                    <thead>
                    <tr >
                        <th>行号</th>
                        <th>工序名称</th>
                        <th>开始时间</th>
                        <th>结束时间</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr ms-repeat="works">
                        <td>{{$index+1}}</td>
                        <td>{{el.workName}}</td>
                        <td>{{el.startTime}}</td>
                        <td>{{el.endTime}}</td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <!-- whitebox -->
</div>
<script type="text/javascript">
    var vm;
    avalon.ready(function () {
        vm = avalon.define({
            $id: "main",
            datas: [],//表格数据
            works: [],
            stations: [],
            trainCode: '',
            currentPage: 1,
            pageSize: 15,
            searchKey: {
                code: ""
            },
            loadTableData: function (curr) {
                var dataStr = vm.$model.searchKey;
                dataStr.currentPage=vm.$model.currentPage;
                dataStr.pageSize=vm.$model.pageSize;
                $.ajax({
                    type: 'post',
                    url: '${ctx}/trainController/selectOnePage.action',
                    data: dataStr,
                    dataType: "json",
                    timeout: 10000,
                    beforeSend: function () {
                        top.layer.msg('正在加载数据...', {
                            icon: 16,
                            shade: 0.01
                        });
                    },
                    success: function (data) {
                        top.layer.closeAll();
                        vm.datas = data.result;
                        layui.laypage({
                            cont: 'demo',
                            pages: data.totalPage,
                            curr: curr || 1, //当前页
                            skip: true,
                            skin: '#1E9FFF',
                            jump: function (obj, first) {
                                if (!first) {
                                    vm.currentPage = obj.curr;
                                    vm.loadTableData(obj.curr);
                                }
                            }
                        });
                    }
                });
            }
        });
        vm.loadTableData();
        avalon.scan();
    });
    //新增车次
    function addRecord() {
        var tabname = "新增车次";
        var text = "新增车次";
        var opentype = "0";
        var href = "${ctx}/trainController/toTrainForm.action";
        top.maincontab.opentabs(href, text, opentype, tabname);
    }
    //编辑车次
    function editRecord(el) {
        var tabname = "编辑车次";
        var text = "编辑车次";
        var opentype = "0";
        var href = "${ctx}/trainController/toTrainForm.action?id=" + el.id;
        top.maincontab.opentabs(href, text, opentype, tabname);
    }
    //编辑车次
    function copyRecord(el) {
        var tabname = "复制车次";
        var text = "复制车次";
        var opentype = "0";
        var href = "${ctx}/trainController/toTrainFormCopy.action?id=" + el.id;
        top.maincontab.opentabs(href, text, opentype, tabname);
    }
    //查询工序
    function selectWorks(el) {
        vm.trainCode = el.code;
        $.ajax({
            type: 'post',
            url: '${ctx}/trainController/selectAllTrainWork.action?trainId=' + el.id,
            data: "",
            dataType: "json",
            timeout: 10000,
            success: function (data) {
                if (data.code == 0) {
                    vm.works = data.trainWorks;
                    vm.stations = data.trainStations;
                } else if (data.code == -1) {
                    vm.works = [];
                    vm.stations = [];
                }

            }
        });
    }
    //删除记录
    function deleteRecord(el) {
        layui.layer.alert('你确定删除该车次吗?', {
            title: '提醒',
            btn: ['确定', '取消']
        }, function () {
            //执行删除的ajax
            $.ajax({
                type: 'post',
                url: '${ctx}/trainController/deleteTrain.action?id=' + el.id,
                data: "",
                dataType: "json",
                timeout: 10000,
                success: function (data) {
                    if (data.code == 0) {
                        layer.msg('删除成功', {icon: 1});
                        vm.loadTableData();
                    } else if (data.code == -1) {
                        layer.msg('删除失败,请重试', {icon: 2});
                    }

                }
            });
        });

    }
    //加载搜索的数据
    function loadSearchData() {
        vm.loadTableData();
    }
</script>
</body>
</html>